package com.ucl.web.controller.flowsearch;

import com.ucl.model.common.ApiResult;
import com.ucl.model.request.flowsearch.FlowRequestOptionsQueryPara;
import com.ucl.model.request.flowsearch.flowDataUpdateRequest;
import com.ucl.service.flowsearch.FlowDataService;
import com.ucl.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/api/datamanage/update")
public class FlowDataMaintainController extends BaseController {

    @Autowired
    private FlowDataService flowDataService;

    @RequestMapping("getRequestOptions")
    public ApiResult getRequestOptions(@RequestBody FlowRequestOptionsQueryPara filter) throws Exception {
        return flowDataService.getRequestOptions(filter);
    }

    @RequestMapping("getProducts")
    public ApiResult getProducts() {
        return new ApiResult(flowDataService.getProducts());
    }

    @RequestMapping("getTableColumnNames")
    public ApiResult getTableColumnNames(@RequestBody Map<String, String> paramMap) {
        System.out.println(paramMap);
        return new ApiResult(flowDataService.getTableColumnNames(paramMap.get("tableName")));
    }

    @RequestMapping("flowDataUpdate")
    public ApiResult flowDataUpdate(@Validated @RequestBody flowDataUpdateRequest filter, BindingResult result) throws Exception {
        int rows = flowDataService.flowDataUpdate(filter);
        if (rows!=0) {
            return ApiResult.ok("更新成功");
        } else {
            return ApiResult.fail("更新失败");
        }
    }

    @RequestMapping("flowDataDelete")
    public ApiResult flowDataDelete(@Valid @RequestBody flowDataUpdateRequest filter) throws Exception {
        int rows = flowDataService.flowDataDelete(filter);
        if (rows!=0) {
            return ApiResult.ok("更新成功");
        } else {
            return ApiResult.fail("更新失败");
        }
    }
}
